package com.xyulu.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.xyulu.entity.SysUserEntity;
import com.xyulu.entity.common.MessageConstant;
import com.xyulu.entity.common.PageResult;
import com.xyulu.entity.common.Result;
import com.xyulu.entity.vo.UserQuery;
import com.xyulu.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @Author luming
 * @Date 2021/4/27
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {

    @Resource
    private SysUserService userService;

    /**
     * 查询用户列表
     * @param userQuery
     * @return
     */
    @PostMapping("/user/list")
    public PageResult getUserList(@RequestBody UserQuery userQuery){
        LambdaQueryWrapper<SysUserEntity> lqw = Wrappers.lambdaQuery();
        //不查询root用户
        lqw.ne(SysUserEntity::getId,1);
        //用户名查询条件
        if (StringUtils.isNotEmpty(userQuery.getUsername())) {
            lqw.like(SysUserEntity::getUsername,userQuery.getUsername());
        }
        //手机号查询条件
        if (StringUtils.isNotEmpty(userQuery.getPhone())) {
            lqw.like(SysUserEntity::getPhone,userQuery.getPhone());
        }
        //状态号查询条件
        if (userQuery.getStatus() != null) {
            lqw.eq(SysUserEntity::getStatus,userQuery.getStatus());
        }
        int count = userService.count(lqw);
        PageHelper.startPage(userQuery.getPage(),userQuery.getLimit());
        List<SysUserEntity> users = userService.list(lqw);
        return new PageResult(200, MessageConstant.USER_FIND_SUCCESS,count,users);
    }

    @GetMapping("/user/changeStatus")
    public Result changeStatus(String status, Integer id){
        Integer change = "true".equals(status)? 1:0;
        try {
            userService.updaStatus(change,id);
            return new Result(200,MessageConstant.USER_STATUS_CHANGE_SUCCESS);
        } catch (Exception e) {
            log.error("用户登录状态修改失败",e);
        }
        return new Result(400,MessageConstant.USER_STATUS_CHANGE_FAIL);
    }

}
